package com.bay1ts.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.bay1ts.dao.UsersMapper;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bay1ts.domain.template.ResetPasswordTemplate;
import com.bay1ts.service.MailService;
@Service("mailService")
public class MailServiceImpl implements MailService {
	@Autowired
	private UsersMapper usersMapper;
	public static String convert(List<ResetPasswordTemplate> dataList) {
	    JSONObject ret = new JSONObject();
	    JSONArray url=new JSONArray();
	    JSONArray to = new JSONArray();

	    for (ResetPasswordTemplate a : dataList) {
	        url.put(a.getUrl());
	        to.put(a.getEmail());
	    }
	    JSONObject sub = new JSONObject();
	    sub.put("%url%", url);
	    
	    ret.put("to", to);
	    ret.put("sub", sub);
	    System.out.println("mailservice里面的就不用去邮箱看了，直接能看到url"+ret.toString());
	    return ret.toString();
	}

	/** 
	 * @author bay1ts
	 * @email bay1ts@gmail.com
	 * 这里面有生成链接的功能，现在把生成的链接的主域名写死在里面了，应该抽象出一个生成链接的函数来，里面从ini里面拿到主域名，反正写死不是个好主意。
	 */
	public boolean sendResetPasswordMail(String email) {
		boolean send=false;
		final String url="https://sendcloud.sohu.com/webapi/mail.send_template.json";
		final String apiUser="bay1ts_resetpassword";
		final String apiKey="***";
		List<ResetPasswordTemplate> dataList=new ArrayList<ResetPasswordTemplate>();
		String resetPasswordUrl = "localhost:8080/user/confirm?code="+usersMapper.getCodeByEmail(email);
		dataList.add(new ResetPasswordTemplate(resetPasswordUrl,email));

		String vars=convert(dataList);
		HttpClient httpclient=new DefaultHttpClient();
		HttpPost httppost=new HttpPost(url);
		List<NameValuePair> nvps =new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("api_user",apiUser));
		nvps.add(new BasicNameValuePair("api_key",apiKey));
		nvps.add(new BasicNameValuePair("from", "bay1ts@gmail.com"));
//		nvps.add(new BasicNameValuePair("to", "bay1ts@gmail.com"));
		nvps.add(new BasicNameValuePair("fromname", "Bay1ts"));
		nvps.add(new BasicNameValuePair("substitution_vars",vars));
		nvps.add(new BasicNameValuePair("template_invoke_name", "test_template_resetpassword"));
		nvps.add(new BasicNameValuePair("subject", "重置密码"));
		nvps.add(new BasicNameValuePair("html", "html"));
		nvps.add(new BasicNameValuePair("resp_email_id", "true"));
		try {
			httppost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			HttpResponse response = httpclient.execute(httppost);
			// 处理响应
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 正常返回
				// 读取xml文档
				send=true;
				String result = EntityUtils.toString(response.getEntity());
				System.out.println(result);
			} else {
				send=false;
				System.err.println("error");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return send;
	}

}
